package com.school.base.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.dao.IStudentDao;
import com.school.base.model.Student;
import com.school.common.constant.Constant;
import com.school.common.tags.PageBean;

public class StudentService {
	private static Logger logger = Logger.getLogger(StudentService.class);
	private IStudentDao studentDao;

	public IStudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(IStudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public Student getStudentById(int id) {
		return studentDao.getStudentById(id);
	}

	public List<Student> getStudentList(Student student, PageBean pageBean) {
		if (pageBean == null) {
			pageBean = new PageBean();
		}
		pageBean.setTotalRecordCount(studentDao.getTotalCount(student));
		return studentDao.getStudentList(student, pageBean.getOffset(), pageBean.getPageSize());
	}

	public void updateStudent(Student student) throws Exception {
		if (student.getRealName() == null || student.getRealName().isEmpty()) {
			logger.error("Insert student fail:realName is empty.");
			throw new Exception("真实姓名不能为空");
		}
		if (student.getGender() == null || student.getGender().isEmpty()) {
			logger.error("Insert student fail:gender is empty.");
			throw new Exception("性别不能为空");
		}
		if (student.getPwd() != null && student.getPwd().length() > 0) {
			if (student.getPwd().length() < Constant.USER_PASSWORD_MIN_LENGTH) {
				logger.error("Inser student fail:pwd length less than " + Constant.USER_PASSWORD_MIN_LENGTH);
				throw new Exception("密码不能小于" + Constant.USER_PASSWORD_MIN_LENGTH + "个字符");
			}
		}
		studentDao.updateStudent(student);
	}

	public void deleteStudent(int id) {
		studentDao.deleteStudent(id);
	}

	public void insertStudent(Student student) throws Exception {
		if (student != null) {
			if (student.getLoginName() == null || student.getLoginName().isEmpty()) {
				logger.error("Insert student fail:LoginName is empty.");
				throw new Exception("登录名不能为空");
			}
			if (this.checkLoginName(student.getLoginName(), student.getId())) {
				logger.error("Insert student fail:LoginName is used.");
				throw new Exception("登录名重复");
			}
			if (student.getPwd() == null || student.getPwd().isEmpty()) {
				logger.error("Insert student fail:Password is empty.");
				throw new Exception("密码不能为空");
			}
			if (student.getPwd().length() < Constant.USER_PASSWORD_MIN_LENGTH) {
				logger.error("Inser student fail:pwd length less than " + Constant.USER_PASSWORD_MIN_LENGTH);
				throw new Exception("密码不能小于" + Constant.USER_PASSWORD_MIN_LENGTH + "个字符");
			}
			if (student.getRealName() == null || student.getRealName().isEmpty()) {
				logger.error("Insert student fail:realName is empty.");
				throw new Exception("真实姓名不能为空");
			}
			if (student.getGender() == null || student.getGender().isEmpty()) {
				logger.error("Insert student fail:gender is empty.");
				throw new Exception("性别不能为空");
			}
			if (student.getClassId() == 0) {
				logger.error("Insert student fail:class is empty.");
				throw new Exception("所属班级不能为空");
			}
			studentDao.insertStudent(student);
		}
	}

	public boolean checkLoginName(String loginName, int studentId) {
		return studentDao.checkLoginName(loginName, studentId);
	}
}
